import string

from django.shortcuts import render
import random
import time
from captcha.image import ImageCaptcha
from django.contrib.auth.handlers import check_password
from django.http import HttpResponse
from rest_framework.response import Response
from rest_framework.views import APIView
from venvs.models import User
import re
import redis
class UserView(APIView):
    # 获取前端用户
    def get(self,request,name):
        # 判断用户是否存在
        if not name:
            return Response({"msg":"用户获取失败",'code':400})
        try:
            # 用户存在,就响应用户存在
            uerte=User.objects.get(username=name)
            return Response({"msg":"用户存在","code":400})
        except Exception as e:
            print(e)
            # 用户不存在,返回
            return Response({"msg":"用户获取成功","code":200})

class ImageCodeView(APIView):
    # 获取前端传来的uuid
    def get(self,request,img_id):
        # 随机数
        stuimg="".join(random.sample(string.ascii_letters+string.digits,4))
        # 生成图片
        imgat=ImageCaptcha()
        # 将随机数在展示
        set=imgat.generate(stuimg)   ####
        # 链接redis
        rdb=redis.Redis(host="127.0.0.1",port=6379,db=2)
        rdb.setex("img_%s"%img_id,60,stuimg)
        return HttpResponse(set,content_type="image/jpg")  ####

class ChecgdgView(APIView):
    # 获取前端用户地址,内容
    def get(self,request):
        imageCodeID=request.query_params.get("imageCodeID")
        imageCode=request.query_params.get("imageCode")
        # 链接redis ,添加图片
        rdb = redis.Redis(host="127.0.0.1", port=6379,db=2)
        img="img_"+ imageCodeID  #####
        reui=rdb.get(img)  ####
        print(">>>>",reui)
        if reui:
            if reui.decode().lower()!=imageCode.lower():
                return Response({"msg":"验证码存在","code":400})
            else:
                return Response({"code":200})
        else:
            return Response({"msg":"验证过期"})

class RoesdieView(APIView):
    def post(self,request):
        userName=request.data.get("userName")
        pwd=request.data.get("pwd")
        mobile=request.data.get("mobile")

        if not all([userName,pwd,mobile]):
            return Response({"msg":"注册存在","code":400})
        User.objects.create_user(username=userName,password=pwd,mobile=mobile)
        return Response({"msg":"注册成功","code":200})

class LoginView(APIView):
    def post(self,request):
        """
        1 获取用户名密码
        2 对前端传来用户名校监
        3 校监成功返回,200状态码登录用户名
        4 校监不成功返回400
        """
        # 1获取用户名密码
        user_name=request.data.get("user")
        pwd=request.data.get("pwd")
        # 2对前端传来用户名校监
        try:
            user=User.objects.get(username=user_name)
        except User.DoesNotExist as e:
            return Response({"mgs":"用户名已存在,请先注册","code":400})
        # 若存在数据库中的密码,是否自定义一致
        ciphertext=check_password(pwd,user.password)  # 数据库是hashere密文储存
        # 不若存在返回成功
        if ciphertext:
            return Response({"mgs":"登录成功","code":200,"user":{"userName":user.username}})
        # 存在返回错误
        else:
            return Response({"mgs":"用户名返回错误","code":400})

